package group5.gwt.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public class KaroTable extends DialogBox implements ClickHandler {
    public static String CELL_WIDTH = "25px";

    private static class Table extends Grid {

	public Table(int numRow, int numCol) {
	    super(numRow, numCol);
	}

	@Override
	public KaroCell getCellForEvent(ClickEvent event) {

	    HTMLTable.Cell cell = super.getCellForEvent(event);
	    KaroCell karoCell = new KaroCell(this, cell.getRowIndex(), cell.getCellIndex());
	    return karoCell;
	}
    }

    private Table grid;

    public KaroTable(int numRow, int numCol) {
	grid = new Table(numRow, numRow);
	CellFormatter cellFormatter = grid.getCellFormatter();
	for (int i = 0; i < numRow; i++) {
	    for (int k = 0; k < numCol; k++) {
		grid.setHTML(i, k, KaroCell.X_SYMBOL);
		cellFormatter.setHeight(i, k, CELL_WIDTH);
		cellFormatter.setWidth(i, k, CELL_WIDTH);
	    }
	}
	grid.addClickHandler(this);
	grid.setStyleName("game-board td");
	grid.setCellSpacing(0);
	setText("Karo Online");
	setWidget(grid);
	setAnimationEnabled(true);
    }

    @Override
    public void onClick(ClickEvent event) {
	KaroCell cell = grid.getCellForEvent(event);
	Window.alert("" + cell.getRowIndex() + " " + cell.getCellIndex());
    }
}
